package com.sobot.finance.poc.web.controller;

import com.sobot.finance.poc.web.controller.base.BaseController;
import com.sobot.finance.poc.web.service.CallbackService;
import com.sobot.finance.poc.web.util.ApiClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 拨打结果推送
 *
 * @author baiyp
 */
@RestController
@Slf4j
public class CallbackController extends BaseController {

    @Resource
    private CallbackService callbackService;

    /**
     * 智齿回调并推送中原金融
     *
     * @param req 请求参数
     * @return
     */
    @RequestMapping("/poc/callback")
    public ApiClient.ApiRes callback(@RequestBody CallbackReq req) {
        log.info("[拨打结果推送-开始]: {}", gson.toJson(req));
        callbackService.callback(req.getResult());
        log.info("[拨打结果推送-结束]: {}", gson.toJson(req));
        return ApiClient.ApiRes.succeed();
    }

    /**
     * 手动推送中原金融
     *
     * @param req 请求参数
     * @return
     */
    @RequestMapping("/manual/callback")
    public ApiClient.ApiRes manualCallback(@RequestBody String req) {
        log.info("[手动结果推送-开始]: {}", gson.toJson(req));
        callbackService.manualCallback(req);
        log.info("[手动结果推送-结束]: {}", gson.toJson(req));
        return ApiClient.ApiRes.succeed();
    }

    /**
     * 手动推送短信
     *
     * @param batchNo 请求参数
     * @return
     */
    @RequestMapping("/manual/sendSms/{batchNo}")
    public ApiClient.ApiRes sendSms(@PathVariable("batchNo") String batchNo) {
        log.info("[手动推送短信-开始]: {}", gson.toJson(batchNo));
        callbackService.sendSms(batchNo);
        log.info("[手动推送短信-结束]: {}", gson.toJson(batchNo));
        return ApiClient.ApiRes.succeed();
    }

    /**
     * @author baiyp
     */
    @Data
    public static class CallbackReq {
        private CallbackResult result;
    }

    /**
     * @author baiyp
     */
    @Data
    public static class CallbackResult {
        private CallbackData data;
    }

    /**
     * @author baiyp
     */
    @Data
    public static class CallbackData {
        private List<CallbackMsg> msg;
        private CallbackDetail detail;
    }

    /**
     * @author baiyp
     */
    @Data
    public static class CallbackMsg {
        private String callID;
        private String msg;
        // 对话⽅: 1-机器⼈ 0-客户
        private Integer senderType;
        private String remark;
    }

    /**
     * @author baiyp
     */
    @Data
    public static class CallbackDetail {
        private String callID;
        // 外呼时：从拨号开始计时，单位：秒
        private Integer callDuration;
        // 微秒量级的时间戳
        private Long answerTime;
        // 从⽤户/机器⼈摘机开始计时，单位：秒
        private Integer feeDuration;
        // 通话录⾳
        private String voiceUrl;
        private String caller;
        // 99:尚未匹配 0:⽆意向,1:A类意向,2:B类意向,3:C类意 向, 4:D类意向,5:E类意向
        private String intentionCategory;
        // 99:尚未匹配 0:⽆意向,1:A类意向,2:B类意向,3:C类意 向, 4:D类意向,5:E类意向
        private Integer intentionCategoryId;
        // ⽤户消息数
        private Integer userMsgCount;
        // 微秒量级的时间戳
        private Long startTime;
        private String tel;
        private String outId;
        // 微秒量级的时间戳
        private Long endTime;
        // 命中的意向。若命中多个是以','间隔
        private String intentionName;
        // 0:未拨打 1:拨打中 2:已接通 3:⽆⼈接听 4:关机 5:停号 6:空号 7:拒接 9:⽤户正忙
        private Integer status;
        private String templateId;
        private String templateName;
        private String taskId;
        private String taskName;
    }
}


